package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private DishService dishService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public R save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    /**
     * 分页查询套餐
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page<SetmealDto>> page(Integer page,Integer pageSize,String name){
        Page<SetmealDto> pageInfo=setmealService.pageSetmealDto(page,pageSize,name);
        return R.success(pageInfo);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public R<String> deleteByIds(Long[]ids){
        setmealService.deleteByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 根据Id查询套餐信息
     * @param id
     * @return
     */
    @GetMapping("{id}")

    public R<SetmealDto>getById(@PathVariable Long id){
       SetmealDto setmealDto= setmealService.getSetmealByIdWithDish(id);
        return R.success(setmealDto);
    }

    /**
     * 更新套餐
     * @param setmealDto
     * @return
     */
    @PutMapping

    public  R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }


    /**
     * 启用禁用
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("status/{status}")

    public R<String> updateStatus(@PathVariable Integer status,Long[] ids){
        //批量修改状态
        setmealService.lambdaUpdate()
                    .set(Setmeal::getStatus,status)
                    .in(Setmeal::getId,ids)
                    .update();
        return R.success("修改成功");
    }

    /**
     * g根据分类查询套餐列表
     * @param categoryId
     * @param status
     * @returngdvfgfggggggggg
     */
    @GetMapping("list")
    @Cacheable(cacheNames = "setmeal",key = "#categoryId+'_'+ #status",unless = "#result.data.size()==0")
    public R<List<Setmeal>> list(Long categoryId,Integer status){
        List<Setmeal> list = setmealService.lambdaQuery()
                .eq(categoryId!=null,Setmeal::getCategoryId, categoryId)
                .eq(Setmeal::getStatus, 1)
                .list();
        return R.success(list);
    }

    /**
     * 根据id查询对应菜品信息
     * @param setmealId
     * @return
     */
    @GetMapping("dish/{setmealId}")

    public  R<List<DishDto>> getDishBySetmealId(@PathVariable Long setmealId){
        List<DishDto> dishList =  dishService.getDishSetmealId(setmealId);
    return R.success(dishList);
    }


}
